package com.leo.basic.scheduler.dao;

import com.leo.basic.scheduler.bean.OperLog;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface OperLogMapper {

    @Insert({"insert into ${tableName} (JOB_NAME, OPER_DESCR, OPER_PARAM, OPER_RESULT, " +
            "OPER_TYPE, START_TIME, END_TIME, DURATION, SUCCESS, TRIGGER_NAME, USER_ID, " +
            "USER_NAME, BEAN_CLASS, METHOD_NAME, IP) " +
            "values (#{jobName,jdbcType=VARCHAR},#{operDescr,jdbcType=VARCHAR},#{operParam,jdbcType=VARCHAR}," +
            "#{operResult,jdbcType=VARCHAR},#{operTime,jdbcType=VARCHAR},#{startTime,jdbcType=TIMESTAMP},#{endTime,jdbcType=TIMESTAMP}," +
            "#{duration,jdbcType=DECIMAL},#{success,jdbcType=VARCHAR},#{triggerName,jdbcType=VARCHAR},#{userId,jdbcType=VARCHAR}," +
            "#{userName,jdbcType=VARCHAR},#{beanClass,jdbcType=VARCHAR},#{methodName,jdbcType=VARCHAR},#{ip,jdbcType=VARCHAR},)"})
    void insert(String tableName, OperLog operLog);

    @Select({"select ID, JOB_NAME, OPER_DESCR, OPER_PARAM, OPER_RESULT,OPER_TYPE, START_TIME, END_TIME, " +
            "DURATION, SUCCESS, TRIGGER_NAME, USER_ID," +
            "USER_NAME, BEAN_CLASS, METHOD_NAME, IP from ${tableName} order by START_TIME desc"})
    @Results({
            @Result(column = "ID", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "JOB_NAME", property = "jobName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "OPER_DESCR", property = "operDescr", jdbcType = JdbcType.VARCHAR),
            @Result(column = "OPER_PARAM", property = "operParam", jdbcType = JdbcType.VARCHAR),
            @Result(column = "OPER_RESULT", property = "operResult", jdbcType = JdbcType.VARCHAR),
            @Result(column = "OPER_TYPE", property = "operType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "START_TIME", property = "startTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "END_TIME", property = "endTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "DURATION", property = "duration", jdbcType = JdbcType.DECIMAL),
            @Result(column = "SUCCESS", property = "success", jdbcType = JdbcType.VARCHAR),
            @Result(column = "TRIGGER_NAME", property = "triggerName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "USER_ID", property = "userId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "USER_NAME", property = "userName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "BEAN_CLASS", property = "beanClass", jdbcType = JdbcType.VARCHAR),
            @Result(column = "METHOD_NAME", property = "methodName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "IP", property = "ip", jdbcType = JdbcType.VARCHAR)
    })
    List<OperLog> getOperLogListByJobName(String tableName, String jobName);
}
